# -*- coding: utf-8 -*-
from optparse import OptionParser
from urllib import quote

def create_option_parser():
    parser = OptionParser()

    parser.remove_option('-h')
    parser.add_option('-h', '--host', dest='host', default='localhost',
                      help='Connect to the database server on the given host.')
    parser.add_option('-u', '--user', dest='user',
                      help='The user name to use when connecting to the database server.')
    parser.add_option('-p', '--password', dest='password',
                      help='The password to use when connecting to the database server.')
    parser.add_option('-P', '--port', dest='port', type='int',
                      help='The TCP/IP port number to use for the connection.')
    parser.add_option('-d', '--database', dest='database', default='pdns',
                      help='The database name to use.')
    parser.add_option('-c', '--config', dest='config',
                      help='The configuration file to use.')
    parser.add_option('', '--help',
                      action='help', help='Show this help.')

    return parser

def create_connection_string(options):
    connstr = 'mysql://'
    if (options.user != None):
        connstr += quote(options.user)
        if (options.password != None):
            connstr += (':' + quote(options.password))
        connstr += '@'
    connstr += quote(options.host)
    if (options.port != None):
        connstr += (':' + str(options.port))
    connstr += '/'
    if (options.database != None):
        connstr += quote(options.database)
    return connstr
